﻿using System;
using System.Collections.Generic;
using System.Text;

public class SafeValue
    {
        public static decimal ChinaRound(decimal v, int decimals)
        {
            //value = double.Parse(value.ToString());
            if (v < 0)
            {
                //," "return Math.Round(v + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
            }
            else
            {
                return Math.Round(v, decimals, MidpointRounding.AwayFromZero);
            }
            return v;
        }

        public static decimal SafeDecimal(object o, decimal exValue)
        {
            decimal value = 0;
            try
            {
                value = Convert.ToDecimal(o);
            }
            catch
            {
                value = exValue;
            }
            return value;
        }
        public static double SafeDouble(object o, double exValue)
        {
            double value = 0;
            try
            {
                value = Convert.ToDouble(o);
            }
            catch
            {
                value = exValue;
            }
            return value;
        }

        public static string SafeString(object o, string exValue)
        {
            string value = "";
            try
            {
                value = o.ToString().Replace("'", "").Trim();
            }
            catch
            {
                value = exValue;
            }
            if (value.Length == 0)
                value = exValue;
            return value;
        }
        public static string SafeStringNoEmpty(object o)
        {
            string value = "";
            try
            {
                value = o.ToString().Replace("'", "").Trim();
            }
            catch
            {
                value = " ";
            }
            if (value.Length == 0)
                value = " ";
            return value;
        }

        public static DateTime SafeExcelDate(object o)
        {
            TimeSpan datefromexcel = new TimeSpan(Convert.ToInt32(o) - 2, 0, 0, 0);
            //val[1,1] is the cell value that is extracted from excel.
            DateTime d = new DateTime(1900, 1, 1).Add(datefromexcel);
            return d;
        }
        public static DateTime SafeDate(object o, DateTime exDate)
        {
            try
            {
                exDate = Convert.ToDateTime(o);
            }
            catch { }
            return exDate;
        }
        public static string SafeDateOracle(object o,string comp)
        {
                string s = "";
            try
            {
                s = o.ToString();
                if (s.Substring(0, comp.Length) == comp)
                    s = "";
                else
                    s = "(" + s + ")";
            }
            catch { }
            
            return s;
        }

        public static int SafeInt(object o, int exValue)
        {
            int value = 0;
            try
            {
                value = Convert.ToInt32(o); // int.Parse(o.ToString());
            }
            catch
            {
                value = exValue;
            }
            return value;
        }
        public static bool SafeBool(object o, bool exValue)
        {
            bool value =true;
            try
            {
                value = Convert.ToBoolean(o); // int.Parse(o.ToString());
            }
            catch
            {
                value = exValue;
            }
            return value;
        }
     }
